<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 产生所有可能的结果
        let generatePossibleResults = (a, b) => {
            let res = [a + b, a - b, b - a, a * b];
            if (a) {
                res.push(b / a);
            }
            if (b) {
                res.push(a / b);
            }
            return res;
        }

        let checkIfResultReached = list => {
            // 结束条件
            if (list.length == 1) {
                return Math.abs(list[0] - 24.0) <= 0.1;
            }

            for (let i = 0; i < list.length; i++) {
                for (let j = i + 1; j < list.length; j++) {
                    // 除i、j外，剩下的数放入newList
                    let newList = [];
                    for (let k = 0; k < list.length; k++) {
                        if (k != i && k != j) {
                            newList.push(list[k]);
                        }
                    }

                    let results = generatePossibleResults(list[i], list[j]);
                    
                    for (let resIdx = 0; resIdx < results.length; ++resIdx) {
                        newList.push(results[resIdx]);

                        if (checkIfResultReached(newList)) {
                            return true;
                        }

                        // 失败则删除当前结果
                        newList.pop();
                    }
                }
            }
            return false;
        };

        let judgePoint24 = cards => checkIfResultReached(cards);
    </script>
</body>

</html>